package com.itheima.esdemo;

import com.alibaba.fastjson.JSON;
import com.itheima.esdemo.pojo.User;
import org.apache.http.HttpHost;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest;
import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequest;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestClient;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
 * 操作文档，crud
 */
public class EsDemo02 {
    private RestHighLevelClient restHighLevelClient;
    //配置客户端连接地址
    @Before
    public void init(){
        restHighLevelClient = new RestHighLevelClient(
                RestClient.builder(
                        // 要连接的es地址
                        new HttpHost("192.168.200.150",9200,"http")
                       ,new HttpHost("192.168.200.151",9200,"http")   // 如果是集群的话可以写多个
                      ,new HttpHost("192.168.200.152",9200,"http")
                      ,new HttpHost("192.168.200.153",9200,"http")
                )
        );
    }

    //批量导入文档
    @Test
    public void bulkAddDocument() throws IOException {
        //从数据库获取集合数据 这里模拟一下
        List<User> userList = Arrays.asList(new User(1L, "Rose", 18, "1", "Rose同学在传智播客学表演"),
                new User(2L, "Jack", 38, "1", "Jack同学在黑马学JavaEE"),
                new User(3L, "小红", 23, "0", "小红同学在传智播客学唱歌"),
                new User(4L, "小明", 20, "1", "小明同学在黑马学JavaSE"),
                new User(5L, "达摩", 33, "1", "达摩和尚在达摩院学唱歌"),
                new User(6L, "鲁班", 24, "1", "鲁班同学走在乡间小路上"),
                new User(7L, "孙尚香", 26, "0", "孙尚香同学想带阿斗回东吴"),
                new User(8L, "李白", 27, "1", "李白同学在山顶喝着酒唱着歌"),
                new User(9L, "甄姬", 28, "0", "甄姬同学弹奏一曲东风破"),
                new User(10L, "虞姬", 27, "0", "虞姬同学在和项羽谈情说爱"));

        //1.创建请求对象
        BulkRequest request = new BulkRequest("user");
        for (User user : userList) {
            IndexRequest indexRequest = new IndexRequest();
            indexRequest.id(user.getId().toString());//设置文档id
            indexRequest.source(JSON.toJSONString(user), XContentType.JSON);//设置文档内容
            request.add(indexRequest);
        }
        //2.调用客户端执行请求
        BulkResponse bulk = restHighLevelClient.bulk(request, RequestOptions.DEFAULT);
        //3.解析结果
        System.out.println(bulk.status());
    }

    //添加单条文档
    @Test
    public void addDocument() throws IOException {
        User user = new User(1L,"鲁班",20,"1","鲁班在河道唱着歌");
        //1.创建请求对象
        IndexRequest request = new IndexRequest("user");
        request.id(user.getId().toString());
        request.source(JSON.toJSONString(user), XContentType.JSON);
        //2.调用客户端执行请求
        IndexResponse response = restHighLevelClient.index(request, RequestOptions.DEFAULT);
        //3.解析结果
        System.out.println(response.getResult());
    }

    //根据ID查询文档
    @Test
    public void findDocumentById() throws IOException {

        //1.创建请求对象
        GetRequest request = new GetRequest("user", "1");
        //2.调用客户端处理请求
        GetResponse response = restHighLevelClient.get(request, RequestOptions.DEFAULT);
        //3.解析结果
        String userJson = response.getSourceAsString();//结果为json格式
       User user = JSON.parseObject(userJson, User.class);
        System.out.println("user = " + user);
    }

    //根据ID删除文档
    @Test
    public void deleteById() throws IOException {
        //1.创建请求对象
        DeleteRequest request = new DeleteRequest("user","1");
        //2.调用客户端处理请求
        DeleteResponse response = restHighLevelClient.delete(request, RequestOptions.DEFAULT);
        //3.解析结果
        System.out.println(response.getResult());
    }


    //关闭客户端
    @After
    public void close() throws IOException {
        restHighLevelClient.close();
    }


}
